package com.treasure.hunt.popup

import android.content.Context
import android.view.View
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import com.module.lemlin.base.loadCropImage
import com.treasure.hunt.R
import com.treasure.hunt.app.AppConstance
import kotlinx.android.synthetic.main.popup_dialog_turn.view.*
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AlphaConfig
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.ScaleConfig

class DialogTurnPopup(
    context: Context,
    val onConfirm: (view: View) -> Unit,
    val onCancel: (view: View) -> Unit
) : BasePopupWindow(context) {


    init {
        setBlurBackgroundEnable(true)
    }

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_dialog_turn)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.tvDialogTurnConfirm.setOnClickListener {
            this.onConfirm(it)
        }
        contentView.tvDialogTurnCancel.setOnClickListener {
            this.onCancel(it)
        }
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withAlpha(AlphaConfig.IN)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withAlpha(AlphaConfig.OUT)
            .toDismiss()
    }

    fun setTextTitle(text: String): DialogTurnPopup {
        contentView.tvDialogTurnTitle.text = text
        return this
    }

    fun setTextMessage(text: String): DialogTurnPopup {
        contentView.tvDialogTurnMessage.text = text
        return this
    }

    fun setTextConfirm(text: String): DialogTurnPopup {
        contentView.tvDialogTurnConfirm.text = text
        return this
    }

    fun setTextCancel(text: String): DialogTurnPopup {
        contentView.tvDialogTurnCancel.text = text
        return this
    }

    fun setData(
        textTitle: String, textMessage: String,
        textConfirm: String, textCancel: String, src: String, srcExchange: String
    ): DialogTurnPopup {
        contentView.tvDialogTurnTitle.text = textTitle
        contentView.tvDialogTurnMessage.text = textMessage
        contentView.tvDialogTurnConfirm.text = textConfirm
        contentView.tvDialogTurnCancel.text = textCancel
        val isHttps: Boolean = src.substring(0, 5).contains("http") ||
                src.substring(0, 5).contains("https")
        contentView.ivDialogTurnIcon.loadCropImage("${if (isHttps) "" else AppConstance.URL_IMAGE}${src}")
        val isHttps2: Boolean = src.substring(0, 5).contains("http") ||
                src.substring(0, 5).contains("https")
        contentView.ivDialogTurnIconExchange.loadCropImage("${if (isHttps2) "" else AppConstance.URL_IMAGE}${srcExchange}")
        return this
    }
}